$(function () {
    


    // if browser doesn't support WebSocket, just show some notification and exit
    if (!window.WebSocket) {
        content.html($('<p>', { text: 'Sorry, but your browser doesn\'t '
                                    + 'support WebSockets.'} ));
        input.hide();
        $('span').hide();
        return;
    }

    // open connection
    var connection = new WebSocket('ws://' + window.location.host + '/SNS/Shooting3DWebSocket.servlet');


    connection.onopen = function () {
        // first we want users to enter their names
        alert("Open connection!\n");
    };

    connection.onerror = function (error) {
        // just in there were some problems with conenction...
        content.html($('<p>', { text: 'Sorry, but there\'s some problem with your '
                                    + 'connection or the server is down.</p>' } ));
    };

    // most important part - incoming messages
    connection.onmessage = function (message) {
    	var array = (""+message.data).split(",");
		position_x=array[0];
		position_y=array[1];
		position_z=array[2];
//         try {
//            var json = JSON.parse(message.data);
//        } catch (e) {
//            return;
//        }
//		if(json.type=='position'){
//			var s=""+json.data;
//			var array = s.split(",");
//			position_x=array[0];
//			position_y=array[1];
//			position_z=array[2];
//			alert(array[0]+" "+array[1]+ " "+array[2]);
//		}
    };

	setInterval(function() {
        connection.send(cam.position.x + "," + cam.position.y +","+ cam.position.z);
    }, 1000);

});